package com.wisemoresystem.seal.action.mac.wktime;

import java.sql.Time;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac_wktime;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.MacWktimeService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印控仪工作时间管理
 * 
 * @author Administrator
 *
 */
@Controller("ixMacWktime")
@Scope("prototype")
@RequestMapping("/ix/mac/wktime")
public class MacWktimeAction extends ProjBaseAction {
	@Autowired
	private MacWktimeService wService;
	@Autowired
	private BankService bService;

	/**
	 * 印控仪工作时间列表
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String bank_id) {

		List<Map<String, Object>> bankList = bService.selectUserBank();
		String bankJson = JsonTools.toJson(bankList);
		List<Map<String, Object>> list = wService.selectMacWktime(bank_id);

		ModelAndView mv = new ModelAndView("/jsp/wktime/mac/list");
		mv.addObject("list", list);
		mv.addObject("bankJson", bankJson);
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 去向添加页面
	 * 
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/add_form")
	public ModelAndView add_form(String bank_id) {

		t_bank bank = wService.findObjById(bank_id, t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/wktime/mac/add_form");
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 印控仪工作时间添加
	 * 
	 * @param start_date
	 *            开始日期
	 * @param end_date
	 *            结束日期
	 * @param start_time
	 *            开始时间
	 * @param end_time
	 *            结束时间
	 * @param bank_id
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(String start_date, String end_date,
			String start_time, String end_time, String bank_id) {

		t_mac_wktime wk = new t_mac_wktime();
		wk.setUser_id(WebTools.getCurrentUserId());
		wk.setBank_id(bank_id);
		wk.setStart_date(DateTools.strToDate(start_date));
		wk.setEnd_date(DateTools.strToDate(end_date));
		wk.setStart_time(Time.valueOf(start_time));
		wk.setEnd_time(Time.valueOf(end_time));
		wService.saveObj(wk);

		logT.logH("添加印控仪特殊工作时间", t_mac_wktime.class, wk.get_id());

		super.setTipMsg("保存成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", bank_id);
		return mv;
	}

	/**
	 * 修改的印控仪时间详细信息
	 * 
	 * @param wktime_id
	 *            时间id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String wktime_id) {

		t_mac_wktime wk = (t_mac_wktime) oc.findObjById(wktime_id,
				t_mac_wktime.class);
		t_bank bank = wService.findObjById(wk.getBank_id(), t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/wktime/mac/edit_form");
		mv.addObject("wk", wk);
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 印控仪修改
	 * 
	 * @param wktime_id
	 *            时间id
	 * @param start_date
	 *            开始日期
	 * @param end_date
	 *            结束日期
	 * @param start_time
	 *            开始时间
	 * @param end_time
	 *            结束时间
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(String wktime_id, String start_date,
			String end_date, String start_time, String end_time) {

		t_mac_wktime wk = (t_mac_wktime) oc.findObjById(wktime_id,
				t_mac_wktime.class);
		wk.setStart_date(DateTools.strToDate(start_date));
		wk.setEnd_date(DateTools.strToDate(end_date));
		wk.setStart_time(Time.valueOf(start_time));
		wk.setEnd_time(Time.valueOf(end_time));
		wService.updateObj(wk);

		logT.logH("修改印控仪特殊工作时间", t_mac_wktime.class, wk.get_id());

		super.setTipMsg("修改成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", wk.getBank_id());
		return mv;
	}

	/**
	 * 印控仪工作时间删除
	 * 
	 * @param wktime_id
	 *            时间 id
	 * @param bank_id
	 *            机构id
	 * @return
	 */
	@RequestMapping("/del")
	public ModelAndView del(String wktime_id, String bank_id) {

		wService.delObjById(wktime_id, t_mac_wktime.class);
		logT.logH("删除印控仪特殊工作时间", t_mac_wktime.class, wktime_id);

		super.setTipMsg("删除成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		mv.addObject("bank_id", bank_id);
		return mv;
	}
}
